package com.zejson.controller;


import com.zejson.entity.Driver;
import com.zejson.entity.Role;
import com.zejson.entity.TruckTeam;
import com.zejson.entity.User;
import com.zejson.service.DriverService;
import com.zejson.service.RoleService;
import com.zejson.service.TruckTeamService;
import com.zejson.service.UserService;
import com.zejson.util.DateConvert;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Date;
import java.text.ParseException;
/*
 * test DI, IOC
 */

public class SpringTest {

	@Test
	public void testUserInsert() throws ClassNotFoundException {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserService userService = (UserService) applicationContext.getBean("userService");
		User user = new User();
		user.setUsername("123");
		user.setSex((byte) 1);
		user.setAccount("12345678");
		user.setPassword("12345678");
		user.setPhone("1234567");
		user.setEmail("skiffs");
		user.setFkRoleid(4);
		user.setCheckintime(new Date(20120507));
		user.setIsdeleted((byte) 1);
		user.setAltertime(new Date(20120605));
		int res = userService.insertByAutoGeneratePK("UserMapper", user);
		System.out.println(res);;
	}

	@Test
	public void testInsertAutoGPK() throws ClassNotFoundException {
		Role role = new Role();
		role.setRolename("test_5");
		role.setRolepurview("Test_5");
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		RoleService roleService = (RoleService) applicationContext.getBean("roleService");
		int res = roleService.insertByAutoGeneratePK("RoleMapper", role);
		System.out.println(res);
	}

	@Test
	public void testTruckTeamService() throws ClassNotFoundException, NoSuchMethodException {
		TruckTeam truckTeam = new TruckTeam();
		truckTeam.setTeamname("testTeam");
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		TruckTeamService truckTeamService = (TruckTeamService) applicationContext.getBean("truckTeamService");
		int res = truckTeamService.insertByAutoGeneratePK("TruckTeamMapper", truckTeam);
		System.out.println(res);
	}

	@Test
	public void testDriverInsert() throws ClassNotFoundException, ParseException {
		String Name = "12123123";
		String Sex = "0";
		String Birth = "2017-03-03";
		String Phone = "1212121212";
		String IDCard = "1222222222222222";
		String Remark = "None";

		Driver driver = new Driver();
		driver.setName(Name);
		driver.setSex((byte) Integer.parseInt(Sex));
		Date Birth1 = DateConvert.parseDate(Birth);
		System.out.println(DateConvert.dateToString(Birth1));
		driver.setBirth(Birth1);
		driver.setPhone(Phone);
		driver.setIdcard(IDCard);
		driver.setState(0);
		java.util.Date utilDate  =new java.util.Date();
		java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
		driver.setCheckintime(sqlDate);
		driver.setIsdeleted((byte)0);
		driver.setRemark(Remark);
		driver.setDriverid(1);

		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		DriverService driverService =  ((DriverService)applicationContext.getBean("driverService"));
		driverService.updateById("DriverMapper",driver);
	}
}
